package com.linglan.bff.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import com.linglan.bff.vo.app.department.DepartmentDetailVO;
import com.linglan.bff.vo.app.department.DepartmentListVO;
import com.linglan.common.api.R;
import com.linglan.modules.department.dto.result.DepartmentResult;
import com.linglan.modules.department.service.BizDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 科室相关业务模块
 */
@RestController
@RequestMapping("/app/departments")
@Slf4j
public class DepartmentController {
    @Resource
    private BizDepartmentService departmentService;

    /**
     * 查询医院的科室列表
     *
     * @param hospitalId
     * @return
     */
    @RequestMapping("/{hospitalId}/list")
    @SaIgnore
    public R<List<DepartmentListVO>> findAllDepartments(@PathVariable Long hospitalId) {
        List<DepartmentResult> allDepartments = departmentService.findAllDepartments(hospitalId);
        List<DepartmentListVO> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(allDepartments)) {
            for (DepartmentResult allDepartment : allDepartments) {
                DepartmentListVO vo = new DepartmentListVO();
                vo.setId(allDepartment.getId().toString());
                vo.setHospitalId(allDepartment.getHospitalId().toString());
                vo.setName(allDepartment.getName());
                vo.setAvatar(allDepartment.getAvatar());
                vo.setShortAvatar(allDepartment.getShortAvatar());
                voList.add(vo);
            }
        }
        return R.ok(voList);
    }

    /**
     * 查询科室详情
     *
     * @param departmentId
     * @return
     */
    @GetMapping("/{departmentId}")
    @SaIgnore
    public R<DepartmentDetailVO> findById(@PathVariable Long departmentId) {
        DepartmentResult departmentResult = departmentService.findById(departmentId);
        if (departmentResult == null) {
            return R.failed("科室不存在");
        }
        DepartmentDetailVO vo = new DepartmentDetailVO();
        vo.setId(departmentResult.getId().toString());
        vo.setHospitalId(departmentResult.getHospitalId().toString());
        vo.setName(departmentResult.getName());
        vo.setAvatar(departmentResult.getAvatar());
        vo.setShortAvatar(departmentResult.getShortAvatar());
        vo.setIntro(departmentResult.getIntro());
        return R.ok(vo);
    }

}
